package com.hrzyit.whistle.cases.ui.activity

import android.content.Intent
import android.media.AudioManager
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.support.v4.app.ActivityCompat
import android.support.v7.widget.GridLayoutManager
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bigkoo.alertview.AlertView
import com.bigkoo.alertview.OnItemClickListener
import com.github.piasy.rxandroidaudio.PlayConfig
import com.github.piasy.rxandroidaudio.RxAudioPlayer
import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.ext.onClick
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.base.ui.adapter.BaseRecyclerViewAdapter
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.base.utils.DateUtils
import com.hrzyit.provider.common.ProviderConstant
import com.hrzyit.provider.routers.RouterPath
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.data.bean.*
import com.hrzyit.whistle.cases.event.ReturnAudioRecordEvent
import com.hrzyit.whistle.cases.injection.component.DaggerCaseCenterComponent
import com.hrzyit.whistle.cases.injection.module.CaseCenterModule
import com.hrzyit.whistle.cases.injection.module.CaseTypeModule
import com.hrzyit.whistle.cases.injection.module.UploadModule
import com.hrzyit.whistle.cases.presenter.RegisterCasePresenter
import com.hrzyit.whistle.cases.presenter.view.RegisterCaseView
import com.hrzyit.whistle.cases.ui.adapter.ImageUploadViewAdapter
import com.jph.takephoto.app.TakePhoto
import com.jph.takephoto.app.TakePhotoImpl
import com.jph.takephoto.compress.CompressConfig
import com.jph.takephoto.model.TResult
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import io.reactivex.internal.functions.Functions
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_register_case.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast
import java.io.File

/**
 * 新建案件界面
 * @author YangWei
 * @date 2019/3/5
 */
@Route(path = RouterPath.CasesCenter.PATH_REGISTER_CASE)
class RegisterCaseActivity : BaseMvpActivity<RegisterCasePresenter>(), RegisterCaseView, View.OnClickListener,
    TakePhoto.TakeResultListener {

    companion object {
        const val TAG = "RegisterCaseActivity"
    }

    // 组件：拍照
    private lateinit var mTakePhoto: TakePhoto

    // 临时上传文件
    private lateinit var mTempFile: File

    // 案件问题类型
    private var caseQuestionTypeList: MutableList<SysDictEntity>? = null

    // 案件类型：大类
    private var caseOneSortTypeList: MutableList<CategoryEntity>? = null

    // 案件类型：小类
    private var caseSecondSortTypeList: MutableList<CategoryEntity>? = null

    // 立案条件
    private var caseConditionList: MutableList<CategoryDetailsEntity>? = null

    // 案件问题类型返回数据map
    private lateinit var caseQuestionTypeMap: MutableMap<String, String>

    // 案件类型：大类返回数据map
    private lateinit var caseOneSortTypeMap: MutableMap<String, String>

    // 案件类型：小类返回数据map
    private lateinit var caseSecondSortTypeMap: MutableMap<String, String>

    // 案件立案条件返回数据map
    private lateinit var caseConditionMap: MutableMap<String, String>

    // 上报案件实体
    private lateinit var caseInfoEntity: CaseInfoEntity

    // 案件上传图片
    private var caseUploadInfoList: MutableList<UploadInfo> = mutableListOf()

    // 上传图片适配器
    private lateinit var imageUploadViewAdapter: ImageUploadViewAdapter

    // 音频播放器
    private lateinit var mRxAudioPlayer: RxAudioPlayer

    /**
     * Activity 结果回调
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.v(TAG, "resultCode")
        when (resultCode) {
            CaseConstant.RESULT_CODE_MAP -> {
                val longitude = data?.getStringExtra("longitude")
                val latitude = data?.getStringExtra("latitude")
                val caseAddress = data?.getStringExtra("caseAddress")

                // 接收案件地址和坐标
                this.caseInfoEntity.latitude = latitude
                this.caseInfoEntity.longitude = longitude
                this.caseInfoEntity.address = caseAddress
                // 页面渲染案件地址文本
                mCaseAddressGPSCell.getRightView().text = caseAddress
            }
            CaseConstant.RESULT_CODE_RECORD_AUDIO -> {
                // 接收录音返回信息
                val audioUploadInfo = data?.getSerializableExtra("audioUploadInfo")
                mAudioPlayIv.visibility = View.VISIBLE
                // TODO 2019/4/15 - 待完成内容：页面显示播放按钮
                this.caseInfoEntity.uploadAudioFileList!!.add(audioUploadInfo as UploadInfo)
            }
            else -> {
                // 接收 TakePhoto 返回
                mTakePhoto.onActivityResult(requestCode, resultCode, data)
            }

        }
    }

    /**
     * 获取从RecordAudioFragment中获取的数据
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    fun onReturnAudioRecordEvent(event: ReturnAudioRecordEvent) {
        caseUploadInfoList.add(0, event.uploadInfo)
        this.caseInfoEntity.uploadAudioFileList!!.add(event.uploadInfo)
        imageUploadViewAdapter.setData(caseUploadInfoList)
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register_case)
        // EventBus 注册
        EventBus.getDefault().register(this)

        // 初始化 TakePhoto
        mTakePhoto = TakePhotoImpl(this, this)
        mTakePhoto.onCreate(savedInstanceState)

        initFragment()
        initView()
        initData()

    }

    private fun initData() {

        // 案件上报图片组件适配器初始化数据
        imageUploadViewAdapter.setData(caseUploadInfoList)

        // 初始化案件类型相关map
        caseQuestionTypeMap = mutableMapOf()
        caseOneSortTypeMap = mutableMapOf()
        caseSecondSortTypeMap = mutableMapOf()
        caseConditionMap = mutableMapOf()
        caseInfoEntity.uploadAudioFileList = mutableListOf()

        // 获取案件问题类型
        mPresenter.getCaseQuestionType()
    }

    /**
     * 初始化类型选择器 Fragment
     */
    private fun initFragment() {
    }

    /**
     * 初始化视图
     */
    private fun initView() {
        // 初始化新建案件实体
        this.caseInfoEntity = CaseInfoEntity()

        this.caseInfoEntity.userId = AppPrefsUtils.getLong(BaseConstant.KEY_SP_USER_TOKEN)
        this.caseInfoEntity.toDept = AppPrefsUtils.getLong(ProviderConstant.KEY_SP_REPORT_DEPT_ID)
        this.caseInfoEntity.toDeptText = AppPrefsUtils.getString(ProviderConstant.KEY_SP_REPORT_DEPT_NAME)

        // 初始化案件图片上传组件的上传按钮
        this.caseUploadInfoList.add(
            UploadInfo(null, null, BaseConstant.DEFAULT_UPLOAD_SERVER, null, null, null, null, 0, null)
        )


        // 类型、大类、小类数据源

        // 案件问题类型
        mCaseQuestionCell.onClick(this)
        // 案件类型：大类
        mCaseOneSortCell.onClick(this)
        // 案件类型：小类
        mCaseSecondSortCell.onClick(this)
        // 案件立案条件
        mCaseConditionCell.onClick(this)
        // 案件上报单位
        mReportOrganizationCell.onClick(this)
        // 初始化上报单位
        mReportOrganizationCell.getRightView().text = this.caseInfoEntity.toDeptText
        // 案件地址及gps
        mCaseAddressGPSCell.onClick(this)

        // 跳转到录制音频文件界面
//        mRecordAudioToSay.onClick(this)
        // 案件描述音频文件播放
        mAudioPlayIv.onClick(this)

        // 注册上传图片组件
        val layoutManager = GridLayoutManager(this, 4) // 线性布局

        mCaseUploadInfoRv.layoutManager = layoutManager

        imageUploadViewAdapter = ImageUploadViewAdapter(this)
        // 上传的图片可以删除
        imageUploadViewAdapter.isDelete = true

        mCaseUploadInfoRv.adapter = imageUploadViewAdapter

        // 配置适配器中组件的事件
        imageUploadViewAdapter.setOnItemClickListener(object :
            BaseRecyclerViewAdapter.OnItemClickListener<UploadInfo> {
            override fun onItemClick(view: View, item: UploadInfo?, position: Int) {
                when (position + 1 == caseUploadInfoList.size) {
                    true -> {
                        // 弹出添加上传图片组件
                        showCaseAddImageAlertView()
                    }
                    false -> {
                        // 已上传的图片点击显示全图
                        showPhotoScreenView(item!!.imageUrl)
                    }
                }

            }

        })
        // 上报按钮
        mReportBtn.onClick(this)
        // 案件去重按钮
        mRepeatBtn.onClick(this)

        mRxAudioPlayer = RxAudioPlayer.getInstance()

    }

    /**
     * 显示图片全图
     */
    private fun showPhotoScreenView(imageUrl: String) {
//        mShowPhotoFl.visibility = View.VISIBLE
//        mShowPhotoIv.visibility = View.VISIBLE
//        GlideUtils.loadUrlImage(this, imageUrl, mShowPhotoIv)
        Log.d("showImage", imageUrl)
    }

    /**
     * 弹出案件上传图片选择窗口
     */
    private fun showCaseAddImageAlertView() {
        AlertView(
            "上传",
            null,
            "取消",
            null,
            arrayOf("拍照", "从相册中选择", "语音描述"),
            this,
            AlertView.Style.ActionSheet,
            OnItemClickListener { _, position ->
                ActivityCompat.requestPermissions(
                    this@RegisterCaseActivity,
                    arrayOf("android.permission.CAMERA"),
                    17
                )

                // 照片压缩，每次都要调用否则只会执行一次
                val config = CompressConfig.ofDefaultConfig()
                // 待确定压缩比 300k
                config.maxSize = 300 * 1024
                mTakePhoto.onEnableCompress(config, false)
                when (position) {
                    0 -> {
                        // 创建照片临时文件
                        createTempFile()
                        // 拍照
                        mTakePhoto.onPickFromCapture(Uri.fromFile(mTempFile))
                    }
                    1 -> {
                        mTakePhoto.onPickFromGallery()
                    }
                    2 -> {
                        // 显示语音描述界面
                        // TODO 2019/4/23 - 待完成内容：进入语音描述 Fragment
                        startActivityForResult<RegisterMediaActivity>(
                            CaseConstant.RESULT_CODE_RECORD_AUDIO,
                            Pair("fragment_key", CaseConstant.RESULT_CODE_RECORD_AUDIO)
                        )
                    }
                }
            }
        ).show()
    }

    /**
     * Dagger2 注入
     */
    override fun injectComponent() {

        DaggerCaseCenterComponent.builder()
            .activityComponent(mActivityComponent)
            .uploadModule(UploadModule())
            .caseTypeModule(CaseTypeModule())
            .caseCenterModule(CaseCenterModule())
            .build()
            .inject(this)

        // 初始化 Presenter.View
        mPresenter.mView = this
    }


    /**
     * 按钮点击事件
     */
    override fun onClick(view: View) {

        when (view.id) {
            R.id.mAudioPlayIv -> {
                this.caseInfoEntity.uploadAudioFileList?.let {
                    val file = it[0]
                    startPlayAudio(file)
                }!!
            }
            R.id.mCaseQuestionCell -> {
                // 显示案件问题类型选择器
                when (caseQuestionTypeMap.isNullOrEmpty()) {
                    false -> {
                        AlertView(
                            "案件类型",
                            null,
                            "取消",
                            null,
                            caseQuestionTypeMap.values.toTypedArray(),
                            this,
                            AlertView.Style.ActionSheet,
                            OnItemClickListener { _, position ->
                                when (position > -1) {
                                    true -> {
                                        val caseQuestionType = this.caseQuestionTypeList!![position]

                                        if (this.caseInfoEntity.typeName == null ||
                                            this.caseInfoEntity.typeName != caseQuestionType.name
                                        ) {

                                            // 选择问题类型id
                                            this.caseInfoEntity.typeId = caseQuestionType.id

                                            // 选择问题类型名称
                                            this.caseInfoEntity.typeName = caseQuestionType.name

                                            // 显示问题名称
                                            mCaseQuestionCell.getRightView().text = caseQuestionType.name
                                            // 取消已选择的大类和小类
                                            this.caseInfoEntity.oneSortName = null
                                            this.caseInfoEntity.secondSortName = null
                                            this.caseInfoEntity.oneSortId = -1
                                            this.caseInfoEntity.secondSortId = -1
                                            mCaseOneSortCell.getRightView().text = "请选择大类"
                                            mCaseSecondSortCell.getRightView().text = "请选择小类"
                                            caseOneSortTypeMap.clear()
                                            caseSecondSortTypeMap.clear()
                                        }
                                        // 加载案件类型：大类数据
                                        loadCategoryData(CaseConstant.CASE_ONE_SORT_TYPE)
                                    }
                                }
                            }
                        )
                            .show()
                    }
                }
            }
            R.id.mCaseOneSortCell -> {
                // 显示案件类型：大类选择器
                when (caseOneSortTypeMap.isNullOrEmpty()) {
                    false -> {
                        AlertView(
                            "案件大类",
                            null,
                            "取消",
                            null,
                            caseOneSortTypeMap.values.toTypedArray(),
                            this,
                            AlertView.Style.ActionSheet,
                            OnItemClickListener { _, position ->
                                when (position > -1) {
                                    true -> {
                                        val caseOneSortCategory = caseOneSortTypeList!![position]

                                        if (this.caseInfoEntity.oneSortName == null ||
                                            this.caseInfoEntity.oneSortName != caseOneSortCategory.sortName
                                        ) {

                                            // 选择大类id
                                            this.caseInfoEntity.oneSortId = caseOneSortCategory.id
                                            // 选择大类名称
                                            this.caseInfoEntity.oneSortName = caseOneSortCategory.sortName
                                            // 显示大类名称
                                            mCaseOneSortCell.getRightView().text = caseOneSortCategory.sortName
                                            // 取消已选择小类
                                            this.caseInfoEntity.secondSortName = null
                                            this.caseInfoEntity.secondSortId = 0
                                            mCaseSecondSortCell.getRightView().text = "请选择小类"
                                            caseSecondSortTypeMap.clear()

                                        }
                                        // 加载案件类型：小类数据
                                        loadCategoryData(CaseConstant.CASE_SECOND_SORT_TYPE)
                                    }
                                }
                            }

                        ).show()
                    }
                }

            }
            R.id.mCaseSecondSortCell -> {
                // 显示案件类型：小类选择器
                when (caseSecondSortTypeMap.isNullOrEmpty()) {
                    false -> {
                        AlertView(
                            "案件小类",
                            null,
                            "取消",
                            null,
                            caseSecondSortTypeMap.values.toTypedArray(),
                            this,
                            AlertView.Style.ActionSheet,
                            OnItemClickListener { _, position ->
                                when (position > -1) {
                                    true -> {
                                        val caseSecondSortCategory = caseSecondSortTypeList!![position]

                                        if (this.caseInfoEntity.secondSortName == null ||
                                            this.caseInfoEntity.secondSortName != caseSecondSortCategory.sortName
                                        ) {

                                            // 选择小类id
                                            this.caseInfoEntity.secondSortId = caseSecondSortCategory.id
                                            // 选择小类名称
                                            this.caseInfoEntity.secondSortName = caseSecondSortCategory.sortName
                                            // 显示小类名称
                                            mCaseSecondSortCell.getRightView().text = caseSecondSortCategory.sortName

                                            // 取消已选择立案条件
                                            this.caseInfoEntity.caseConditionName = null
                                            this.caseInfoEntity.categoryDetailsId = null
                                            mCaseConditionCell.getRightView().text = "请选择立案条件"
                                            caseConditionList = null
                                            caseConditionMap.clear()

                                        }

                                        // 加载案件立案条件数据
                                        loadCategoryData(CaseConstant.CASE_CONDITION_TYPE)
                                    }
                                }
                            }
                        ).show()
                    }
                }
            }
            R.id.mCaseConditionCell -> {
                // 显示案件立案条件选择器
                when (caseConditionMap.isNullOrEmpty()) {
                    false -> {
                        AlertView(
                            "立案条件",
                            null,
                            "取消",
                            null,
                            caseConditionMap.values.toTypedArray(),
                            this,
                            AlertView.Style.ActionSheet,
                            OnItemClickListener { _, position ->
                                when (position > -1) {
                                    true -> {
                                        val caseCondition = caseConditionList!![position]
                                        // 选择小类id
                                        this.caseInfoEntity.categoryDetailsId = caseCondition.id
                                        // 选择小类名称
                                        this.caseInfoEntity.caseConditionName = caseCondition.caseShortName
                                        // 显示小类名称
                                        mCaseConditionCell.getRightView().text = caseCondition.caseShortName
                                    }
                                }
                            }
                        ).show()
                    }
                }
            }
            R.id.mReportOrganizationCell -> {
                //
                // TODO 2019/4/12 - 待完成内容：显示案件上报单位选择器,目前设置为当前登录人登录时指定单位：指挥中心
            }
            R.id.mCaseAddressGPSCell -> {
                // 显示案件发生地址 GPS 信息
                // 弹出地图组件
                startActivityForResult<RegisterMediaActivity>(
                    CaseConstant.RESULT_CODE_MAP,
                    Pair("fragment_key", CaseConstant.RESULT_CODE_MAP),
                    Pair("longitude", this.caseInfoEntity.longitude),
                    Pair("latitude", this.caseInfoEntity.latitude),
                    Pair("caseAddress", this.caseInfoEntity.address)
                )
            }
//            R.id.mRecordAudioToSay -> {
//                // 录音按键
//                startActivityForResult<RegisterMediaActivity>(
//                    CaseConstant.RESULT_CODE_RECORD_AUDIO,
//                    Pair("fragment_key", CaseConstant.RESULT_CODE_RECORD_AUDIO)
//                )
//            }
            R.id.mReportBtn -> {

                // caseUploadInfoList 其中包含默认上传按钮图片，所以必然不为空
                val uploadInfoList = this.caseUploadInfoList.filterNot {
                    it.getPath.isNullOrBlank()
                }.toMutableList()

                // 案件描述
                Log.d("------hr->", "${mCaseReportDescEt.text}")
                mCaseReportDescEt.text?.let {
                    this.caseInfoEntity.description = it.toString()
                }

                // TODO 验证
                if (this.caseInfoEntity.typeId > 0
                    && this.caseInfoEntity.oneSortId > 0
                    && this.caseInfoEntity.secondSortId > 0
                    && uploadInfoList.isNotEmpty()
                    && uploadInfoList.size > 0
                ) {

                    caseInfoEntity.uploadFileList = uploadInfoList
                    mPresenter.caseApply(caseInfoEntity)
                } else {
                    toast("请输入案件信息")
                }
            }
        }
    }

    private fun startPlayAudio(file: UploadInfo) {
        file.let {
            mRxAudioPlayer.play(
                PlayConfig.url("${file.fileHost}/${file.getGroup}/${file.getPath}")
                    .streamType(AudioManager.STREAM_VOICE_CALL).looping(false)
                    .build()
            ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    Functions.emptyConsumer(),
                    Consumer<Throwable> { it.printStackTrace() },
                    Action { this.startPlayAudio(file) })
        }


    }

    /**
     * 加载案件类型：大类、小类数据
     * @param sortType  案件类型：=1 大类; = 2 小类; = 3 立案条件
     */
    private fun loadCategoryData(sortType: Int) {
        when (sortType) {
            1 -> {
                if (this.caseOneSortTypeList.isNullOrEmpty()) {
                    mPresenter.getOneCategory(this.caseInfoEntity.typeId.toLong())
                }
            }
            2 -> {
                if (this.caseSecondSortTypeList.isNullOrEmpty()) {
                    mPresenter.getSecondCategory(this.caseInfoEntity.oneSortId)
                }
            }
            3 -> {
                if (this.caseConditionList.isNullOrEmpty()) {
                    mPresenter.getCaseConditionList(this.caseInfoEntity.secondSortId)
                }
            }
        }

    }


    /**
     * 获取上传图片成功调用
     */
    override fun takeSuccess(result: TResult?) {
        // 获取成功
        Log.d("获取成功-未压缩", result?.image?.originalPath)
        Log.d("获取成功", result?.image?.compressPath)

//        val photo = result?.image?.originalPath
        val photo = result?.image?.compressPath


        // 添加水印
//        var bitMap:Bitmap = BitmapFactory.decodeFile(photo)
//
//        val width = bitMap.width
//        val height  = bitMap.height
//
//        // 创建新图片
//        var newBitMap:Bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888)
//
//        var canvas:Canvas = Canvas(newBitMap)
//
//        // 向位图中画入原始图片
//        canvas.drawBitmap(bitMap,0.0f,0.0f,null)
//
//        // 添加时间水印
//        var paint:Paint = Paint()
//        paint.color = Color.RED
//        paint.textSize = 24f
//        var now = DateUtils.now
//
//        canvas.drawText(now,0.0f, 0.0f, paint)
//        canvas.save(Canvas.ALL_SAVE_FLAG)
//        canvas.restore()

        // 上传图片到服务器
        photo?.let {
            mPresenter.uploadPhoto(it)
        }

    }

    /**
     * 取消选择图片调用
     */
    override fun takeCancel() {
        // 取消选择
        Log.d("取消选择", "cancel")
    }

    /**
     * 获取图片失败调用
     */
    override fun takeFail(result: TResult?, msg: String?) {
        // 获取失败
        Log.d("获取失败", msg)
    }

    /**
     * 新建案件完成回调
     */
    override fun onCaseApplyResult(data: MutableMap<String, Any>) {
        val caseApplyResult = data["result"] as Boolean
        val count = data["count"] as Double
        when (caseApplyResult) {
            true -> {
                // 返回首页
                toast("上报成功")
                ARouter.getInstance()
                    .build(RouterPath.WhistleApp.PATH_MAIN).navigation()
            }
            else -> {
                when (count > 0) {
                    true -> {
                        toast("上报案件重复")
                    }
                    else -> {
                        toast("上报案件失败，请检查案件信息是否完整")
                    }
                }
            }
        }
    }

    /**
     * 案件问题上传图片完成回调，刷新案件问题上传图片显示区域
     */
    override fun onUploadResult(result: UploadInfo) {

        // 获取服务器返回的图片网络地址信息
//        val url = "${result.fileHost}${result.getGroup}/${result.getPath}"
//        result.addTime = DateUtils.getNow("yyyy/MM/dd HH:mm:ss")
        // 将上传的图片交给 GridViewAddImageAdapter 管理
        // Glide 是反向显示列表数据，所以要在 caseUploadInfoList 的下标0的位置插入新图片对象
        caseUploadInfoList.add(0, result)
        imageUploadViewAdapter.setData(caseUploadInfoList)
    }

    /**
     * 获取案件类型：小类回调
     */
    override fun onGetSecondCategoryResult(t: MutableList<CategoryEntity>) {
        caseSecondSortTypeList = t

        when (t.isNullOrEmpty()) {
            true -> {
                caseSecondSortTypeMap.clear()
            }
            false -> {
                caseSecondSortTypeList?.forEachIndexed { index, it ->
                    caseSecondSortTypeMap["${index}_${it.sortName}"] = it.sortName
                }
            }

        }
    }

    /**
     * 获取案件类型：大类回调
     */
    override fun onGetOneCategoryResult(t: MutableList<CategoryEntity>) {
        caseOneSortTypeList = t
        when (t.isNullOrEmpty()) {
            true -> {

                caseOneSortTypeMap.clear()
            }
            false -> {
                caseOneSortTypeList?.forEachIndexed { index, it ->
                    caseOneSortTypeMap["${index}_${it.sortName}"] = it.sortName
                }
            }
        }


    }

    /**
     * 获取案件类型：问题类型回调
     */
    override fun onGetCaseQuestionTypeResult(t: MutableList<SysDictEntity>) {
        caseQuestionTypeList = t
        caseQuestionTypeList?.forEachIndexed { index, it ->
            caseQuestionTypeMap["${index}_${it.name}"] = it.name
        }
    }

    /**
     * 获取案件立案条件，回调
     */
    override fun onGetCaseConditionListResult(t: MutableList<CategoryDetailsEntity>) {
        caseConditionList = t
        caseConditionList?.forEachIndexed { index, it ->
            caseConditionMap["${index}_${it.caseShortName}"] = it.caseShortName
        }

    }

    /**
     * 创建上传图片临时文件
     */
    private fun createTempFile() {
        val tempFileName = "${DateUtils.curTime}.png"
        when (Environment.MEDIA_MOUNTED) {
            Environment.getExternalStorageState() -> {
                // 在存储卡中创建
                this.mTempFile = File(Environment.getExternalStorageDirectory(), tempFileName)
            }
            else -> {
                // 在内存中创建
                this.mTempFile = File(filesDir, tempFileName)
            }
        }
    }
}
